<?php


namespace EchoPHP\Database\TuxedoDriver;

class Tuxedo {

    protected $server;
    protected $dbId;
    protected $dbType;

    public function __construct($server, $dbType, $dbId = 1) {
        //定义tuxedo服务名
        $this->server = strtoupper($server);
        $this->dbType = strtoupper($dbType);
        $this->dbId = $dbId;
    }

    /**
     * 预处理sql，生成tuxedoStatement
     * @param $sql
     * @return TuxedoStatement
     * @throws \Exception
     */
    public function prepare($sql) {
        return new TuxedoStatement($this->server, $this->dbType, $this->dbId, $sql);
    }

    /**
     * 预处理单库事务
     * @param bool $rollbackIfNoAffects
     * @param array $tuxedoStatements
     * @return TuxedoTransactionStatement
     */
    public function prepareTransaction(bool $rollbackIfNoAffects, array $tuxedoStatements = []) {
        $tuxedoTransactionStatement = new TuxedoTransactionStatement($this->server, $this->dbId, $rollbackIfNoAffects);
        /* @var TuxedoStatement $statement */
        foreach ($tuxedoStatements as $statement) {
            $tuxedoTransactionStatement->pushStatement($statement);
        }
        return $tuxedoTransactionStatement;
    }

    /**
     * 开启tuxedo事务
     * @return bool
     */
    public function beginTransaction() {
        throw new \RuntimeException('TUXEDO暂不支持此类事务操作');
    }

}